package com.ruoyi.business.controller;

import java.security.acl.Group;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.THomeworkStu;
import com.ruoyi.business.domain.TworkStuComment;
import com.ruoyi.business.domain.TworkStuCommentDetail;
import com.ruoyi.business.domain.bo.FinishedHomeWorkBo;
import com.ruoyi.business.domain.bo.GroupCommentBo;
import com.ruoyi.business.domain.vo.CourseHomeWorkVo;
import com.ruoyi.business.domain.vo.StuOtherCommentVo;
import com.ruoyi.business.service.ITworkStuCommentService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 作业互评主Controller
 * 
 * @author 最爱喝兽奶
 * @date 2022-08-14
 */
@RestController
@RequestMapping("/business/othComment")
public class TworkStuCommentController extends BaseController
{
    @Autowired
    private ITworkStuCommentService tworkStuCommentService;

    /**
     * 查询作业互评主列表
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:list')")
    @GetMapping("/list")
    public TableDataInfo list(TworkStuComment tworkStuComment)
    {
        startPage();
        List<TworkStuComment> list = tworkStuCommentService.selectTworkStuCommentList(tworkStuComment);
        return getDataTable(list);
    }

    /**
     * 导出作业互评主列表
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:export')")
    @Log(title = "作业互评主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TworkStuComment tworkStuComment)
    {
        List<TworkStuComment> list = tworkStuCommentService.selectTworkStuCommentList(tworkStuComment);
        ExcelUtil<TworkStuComment> util = new ExcelUtil<TworkStuComment>(TworkStuComment.class);
        util.exportExcel(response, list, "作业互评主数据");
    }

    /**
     * 获取作业互评主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tworkStuCommentService.selectTworkStuCommentById(id));
    }

    /**
     * 新增作业互评主
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:add')")
    @Log(title = "作业互评主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TworkStuComment tworkStuComment)
    {
        return toAjax(tworkStuCommentService.insertTworkStuComment(tworkStuComment));
    }

    /**
     * 修改作业互评主
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:edit')")
    @Log(title = "作业互评主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TworkStuComment tworkStuComment)
    {
        return toAjax(tworkStuCommentService.updateTworkStuComment(tworkStuComment));
    }

    /**
     * 删除作业互评主
     */
    @PreAuthorize("@ss.hasPermi('system:COMMENT:remove')")
    @Log(title = "作业互评主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tworkStuCommentService.deleteTworkStuCommentByIds(ids));
    }


    @ApiOperation("互评分组")
    @PostMapping("/groupOtherComment")
    public AjaxResult groupOtherComment(@Param("courseId") String courseId,
                                            @Param("number") String number) throws Exception {
        GroupCommentBo bo = new GroupCommentBo();
        bo.setNumber(number);
        bo.setCourseId(courseId);
        List<StuOtherCommentVo> stuOtherCommentVos = tworkStuCommentService.groupOtherComment(bo);
//        List<CourseHomeWorkFiVo> list2 = new ArrayList<>();
//        list.forEach(e -> {
//            CourseHomeWorkFiVo fi = new CourseHomeWorkFiVo();
//            BeanUtils.copyProperties(e,fi);
//            if (StringUtils.isNotNull(e.getStuAnswer()) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//            if (StringUtils.isNotNull(e.get) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//
//            list2.add(fi);
//
//
//        });
        return AjaxResult.success(stuOtherCommentVos);


    }

    @ApiOperation("拿到互评list")
    @GetMapping("/getAllCommentDetail")
    public AjaxResult getAllComment() throws Exception {

        FinishedHomeWorkBo bo = new FinishedHomeWorkBo();
        List<StuOtherCommentVo> stuOtherCommentVos = tworkStuCommentService.getAllCommentDetail(bo);
//        List<CourseHomeWorkFiVo> list2 = new ArrayList<>();
//        list.forEach(e -> {
//            CourseHomeWorkFiVo fi = new CourseHomeWorkFiVo();
//            BeanUtils.copyProperties(e,fi);
//            if (StringUtils.isNotNull(e.getStuAnswer()) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//            if (StringUtils.isNotNull(e.get) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//
//            list2.add(fi);
//
//
//        });
        return AjaxResult.success(stuOtherCommentVos);


    }


    @ApiOperation("拿到个人互评list")
    @GetMapping("/getNeedComment")
    public AjaxResult getNeedComment() throws Exception {

        FinishedHomeWorkBo bo = new FinishedHomeWorkBo();
        bo.setWriterId(String.valueOf(SecurityUtils.getUserId()));

        List<StuOtherCommentVo> stuOtherCommentVos = tworkStuCommentService.getAllCommentDetail(bo);
//        List<CourseHomeWorkFiVo> list2 = new ArrayList<>();
//        list.forEach(e -> {
//            CourseHomeWorkFiVo fi = new CourseHomeWorkFiVo();
//            BeanUtils.copyProperties(e,fi);
//            if (StringUtils.isNotNull(e.getStuAnswer()) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//            if (StringUtils.isNotNull(e.get) && "0".equals(e.getType())) {
//                fi.setStuAnswer(Integer.valueOf(e.getStuAnswer()));
//            }
//
//            list2.add(fi);
//
//
//        });
        return AjaxResult.success(stuOtherCommentVos);


    }

    @Log(title = "教师评分")
    @PostMapping("/teacherComment")
    public AjaxResult teacherComment(@Param("courseId") String courseId,
                                     @Param("nameId") String nameId,
                                     @Param("score") String score)
    {
        THomeworkStu t1 = new THomeworkStu();
        t1.setCourseId(courseId);
        t1.setUploadUser(nameId);
        t1.setScore(score);
        return toAjax(tworkStuCommentService.teacherComment(t1));
    }


    @Log(title = "学生评分")
    @PostMapping("/studentComment")
    public AjaxResult studentComment(@Param("id") String id,
                                     @Param("name2Id") String name2Id,
                                     @Param("score") String score)
    {
        TworkStuCommentDetail t1 = new TworkStuCommentDetail();
        t1.setCommentId(id);
        t1.setCommentUser(name2Id);
        t1.setScore(score);
        t1.setCommentTime(DateUtils.getDate());
        t1.setFinished("1");
        return toAjax(tworkStuCommentService.studentComment(t1));
    }


}

